<?php
/**
 * @package     CiMS.framework
 *
 * @copyright   Copyright (C) 2012 Cimaron Shanahan. All rights reserved.
 * @license     http://opensource.org/licenses/mit-license.php MIT License
 */
defined('CiMS') or die;


/**
 * CiMS Request Class
 */
class CiMSRequest extends CiMSObject {

	/**
	 * Override CiMSObject::getInstance()
	 */
	public static function getInstance() {
		return parent::getInstance();
	}

	/**
	 * Constructor
	 */
	public function __construct() {
		parent::__construct();
		parent::set('post',    new CiMSRequestObject($_POST));
		parent::set('get',     new CiMSRequestObject($_GET));
		parent::set('request', new CiMSRequestObject($_REQUEST));
		parent::set('server',  new CiMSRequestObject($_SERVER));
		parent::set('cookie',  new CiMSRequestObject($_COOKIE));
	}

	/**
	 * Overrides CiMSObject::get()
	 *
	 * @param   string   $key       Key, or request array object to return, or NULL to return 'request' array object
	 * @param   mixed    $default   Default value if not present
	 * @param   string   $array     Request object to use (optional)
	 * @param   string   $filter    Filter to apply (optional)
	 *
	 * @return  mixed
	 */
	public function get($key = NULL, $default = NULL, $array = 'request', $filter = NULL) {

		//are we getting a top level array?
		if (is_null($default) && $array == 'request' && is_null($filter)) {

			if (is_null($key)) {
				$key = 'request';
			}

			if ($this->has($key) && !$this->_data['request']->has($key)) {
				return $this->_data[$key];
			}
		}

		if (!parent::has($array)) {
			return $default;
		}

		$value = $this->_data[$array]->get($key, $default);

		if ($filter) {
			//Add filters
		}

		return $value;
	}

	/**
	 * Overrides CiMSObject::set()
	 *
	 * @param   string   $key     Key
	 * @param   mixed    $value   Default value if not present
	 * @param   string   $array   Request object to use (optional)
	 */
	public function set($key, $value, $array = 'request') {

		if (!parent::has($array)) {
			return false;
		}

		$this->_data[$array]->set($key, $value);

		if ($array == 'get' || $array == 'post') {
			$this->_data['request']->set($key, $value);
		}

		return true;
	}

	/**
	 * Overrides CiMSObject::has()
	 *
	 * @param   string   $key     Key
	 * @param   string   $array   Request object to use (optional)
	 *
	 * @return  bool	 
	 */
	public function has($key, $array = 'request') {
		if (!parent::has($array)) {
			return false;
		}
		return $this->_data[$array]->has($key);
	}

	/**
	 * Clear data
	 *
	 * @param   string   $key     Key
	 * @param   string   $array   Request object to use (optional)
	 */
	public function clear($key, $array = 'request') {

		if (!parent::has($array)) {
			return false;
		}

		$this->_data[$array]->clear($key);

		if ($array == 'get' && !$this->_data['post']->has($key)) {
			$this->_data['request']->clear($key);
		}

		if ($array == 'post' && !$this->_data['get']->has($key)) {
			$this->_data['request']->clear($key);
		}

		return true;
	}

	/**
	 * Reset data
	 *
	 * @param   string   $array   Request object to use (optional)
	 */
	public function reset($array = 'request') {

		if (!parent::has($array)) {
			return false;
		}

		$data = $this->_data[$array];
		foreach ($data->_data as $key => $value) {
			$this->clear($key, $array);
		}

		return true;
	}
}


/**
 * CiMS Request Object Class
 */
class CiMSRequestObject extends CiMSObject {

	/**
	 * Constructor
	 */
	public function __construct(&$data) {
		parent::__construct();
		$this->setRef($data);
	}

}

